package bysj.service;


import bysj.dao.GraduateProjectStatusDao;
import bysj.domain.GraduateProject;
import bysj.domain.GraduateProjectStatus;
import util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;

public final class GraduateProjectStatusService {
	private static GraduateProjectStatusDao graduateProjectStatusDao= GraduateProjectStatusDao.getInstance();
	private static GraduateProjectStatusService graduateProjectStatusService=new GraduateProjectStatusService();


	public static GraduateProjectStatusService getInstance(){
		return graduateProjectStatusService;
	}

	public Collection<GraduateProjectStatus> findAll(){
		return graduateProjectStatusDao.findAll();
	}

	public GraduateProjectStatus find(Integer id) throws SQLException{
		//获得连接以便完成可能的事务操作
		Connection connection = JdbcHelper.getConn();
		GraduateProjectStatus graduateProjectStatus=null;
		//将自动提交设为false,开始事务
		connection.setAutoCommit(false);
		try {
			graduateProjectStatus=graduateProjectStatusDao.find(id,connection);
			//提交事务
			connection.commit();
		}catch (SQLException e) {
			//回滚事务中所有操作
			connection.rollback();
		} finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//关闭连接
			JdbcHelper.close(connection);
		}
		return graduateProjectStatus;
	}

	public boolean update(GraduateProjectStatus graduateProjectStatus){
		return graduateProjectStatusDao.update(graduateProjectStatus);
	}

	public boolean add(GraduateProjectStatus graduateProjectStatus){
		return graduateProjectStatusDao.add(graduateProjectStatus);
	}

//	public boolean delete(Integer id){
//		GraduateProjectStatus graduateProjectStatus = this.find(id);
//		return this.delete(graduateProjectStatus);
//	}

//	public boolean delete(GraduateProjectStatus graduateProjectStatus){
//		//获得所有处于本状态的课题（GraduateProject）
//		Collection<GraduateProject> graduateProjectSet = GraduateProjectService.getInstance().findAll(graduateProjectStatus);
//		//若没有处于本状态的课题，则能够删除
//		if(graduateProjectSet.size()==0){
//			return graduateProjectStatusDao.delete(graduateProjectStatus);
//		}else {
//			return false;
//		}
//	}
}
